

export function useBezier(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number): number[][] {
    const h = 100
    const cp = [
        { x: x1, y: y1 + h },
        { x: x2, y: y2 + h },
        { x: x3, y: y3 + h },
        { x: x4, y: y4 + h }
    ]
    const numberOfPoints = 100;
    const curve: { x: number, y: number }[] = [];
    const dt = 1.0 / (numberOfPoints - 1)

    for (let i = 0; i < numberOfPoints; i++) {
        let ax: number, bx: number, cx: number;
        let ay: number, by: number, cy: number;
        let tSquared: number, tCubed: number;
        let result_x: number, result_y: number;

        cx = 3.0 * (cp[1].x - cp[0].x);
        bx = 3.0 * (cp[2].x - cp[1].x) - cx;
        ax = cp[3].x - cp[0].x - cx - bx;


        cy = 3.0 * (cp[1].y - cp[0].y);
        by = 3.0 * (cp[2].y - cp[1].y) - cy;
        ay = cp[3].y - cp[0].y - cy - by;

        const t = dt * i
        tSquared = t * t;
        tCubed = tSquared * t;
        result_x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
        result_y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
        curve[i] = {
            x: result_x,
            y: result_y
        };
    }

    const array: [number, number][] = [];
    for (var i = 0; i < curve.length; i++) {
        try {
            var j = (i < 100) ? i : (199 - i);
            const xx = Math.floor(curve[j].x)
            const yy = Math.floor(Math.abs(100 - curve[j].y))
            array.push([xx, yy])
        } catch (e) {
            break
        }
    }
    return array;
}